package org.spring.springsecrty;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.spring.springsecrty.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.List;

@Service("userDetailsService")
public class UserDetailsService12 implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

         QueryWrapper<org.spring.springsecrty.pojo.User> userQueryWrapper = new QueryWrapper();
        final QueryWrapper<org.spring.springsecrty.pojo.User> username1 = userQueryWrapper.eq("username", username);


        final org.spring.springsecrty.pojo.User user2 = userMapper.selectOne(userQueryWrapper);

        if (user2 ==null)
            throw new UsernameNotFoundException("用户名不存在");
        List<GrantedAuthority> auths =
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_admin");
        return new User(user2.getUsername(),
                new BCryptPasswordEncoder().encode(user2.getPassword()),auths);
    }
}
